﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ElevatorAutoController : MonoBehaviour,IElevatorBtn
{
    [SerializeField] private Elevator elevator;

    public float waitTime;
    public List<Transform> pointList;
    int nextPointIndex = 0;
    Transform currentPoint;

    private int c=1;

    float coolingWatiTime = 0;
    bool isCooling = false;

    void Start()
    {
        ChangePatrolPoint();
        elevator.elevatorBtns.Add(this);
        go();
    }

    // Update is called once per frame

    void ChangePatrolPoint()
    {
        if (pointList.Count > 0)
        {
            currentPoint = pointList[nextPointIndex];
           nextPointIndex += c;
           if (nextPointIndex >= pointList.Count - 1)c = -1;
           if(nextPointIndex<=0) c = 1;
        }
    }


    void Update()
    {
        if (isCooling)
        {
            coolingWatiTime += Time.deltaTime;
            if (coolingWatiTime>=waitTime)
            {
                isCooling = false;
                coolingWatiTime = 0;
            }
        }
        else
        {
            elevator.MoveTo(currentPoint.position, this);
        }
    }
    public void go()
    {
        elevator.MoveTo(currentPoint.position, this);
    }

    public void Off()
    {
        isCooling = true;
        ChangePatrolPoint();
        //throw new System.NotImplementedException();
    }
}
